package com.puman.watchtrade.fragment.info.httpHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.puman.watchtrade.fragment.info.model.InfoData;
import com.puman.watchtrade.fragment.info.model.InfoDatas;
import com.puman.watchtrade.fragment.info.model.InfoImg;
import com.puman.watchtrade.fragment.info.model.InfoImgs;
import com.puman.watchtrade.util.Config;
import com.puman.watchtrade.util.DataResult;
import com.puman.watchtrade.util.HttpClent;

public class InfoHttpHandler {
	/*
	 * 信息列表
	 */
	public DataResult InfoList(String pageNumber) {
		DataResult dataResult = new DataResult();
		InfoDatas infoDatas = new InfoDatas();
		String path = Config.baseUrlStr + Config.InfoList + "pageNumber="
				+ pageNumber;
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				// JSONObject userObject = result.getJSONObject("data");
				JSONArray jsonArray = result.getJSONArray("data");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject userObject = jsonArray.getJSONObject(i);
					InfoData infoData = new InfoData();
					infoData.setId(userObject.getString("id"));
					infoData.setTitle(userObject.getString("title"));
					infoData.setPic(Config.imgUrl+userObject.getString("pic"));
					infoData.setDes(userObject.getString("des"));
					infoData.setUrl(Config.webUrl+userObject.getString("url"));
					infoDatas.infoList.add(infoData);
				}
			}
			dataResult.object = infoDatas;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			dataResult.flag = false;
			e.printStackTrace();
		}
		return dataResult;
	}
	
	/*
	 * 顶部图片轮播
	 */
	public DataResult topImg() {
		DataResult dataResult = new DataResult();
		InfoImgs infoImgs = new InfoImgs();
		String path = Config.baseUrlStr + Config.topImg ;
		String resultJson = HttpClent.getUrlInfo(path);
		JSONObject result;
		try {
			result = new JSONObject(resultJson);
			dataResult.flag = result.getBoolean("flag");
			dataResult.msg = result.getString("msg");
			if (dataResult.flag) {
				// JSONObject userObject = result.getJSONObject("data");
				JSONArray jsonArray = result.getJSONArray("data");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject userObject = jsonArray.getJSONObject(i);
					InfoImg infoImg =new InfoImg();
					infoImg.setId(userObject.getString("id"));
					infoImg.setTitle(userObject.getString("title"));
					if (userObject.getString("pic").equals("")) {
						infoImg.setPic("");
					}else {
						infoImg.setPic(Config.imgUrl+userObject.getString("pic"));
					}
					infoImg.setDes(userObject.getString("des"));
					infoImg.setIsTop(userObject.getString("isTop"));//0:否/1:是
					infoImg.setUrl(Config.webUrl+userObject.getString("url"));
					infoImgs.imgList.add(infoImg);
				}
			}
			dataResult.object = infoImgs;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			dataResult.flag = false;
			e.printStackTrace();
		}
		return dataResult;
	}


}
